from django import template
register = template.Library()
from django.utils.html import format_html

# 自定义页面优化显示标签
@register.simple_tag
def PageShow(count,request):
    print(count)
    page = int(request.GET.get('p',1))
    print(page)
    res = request.GET.dict()
    print(res)
    args = ''
    if 'types' in res.keys():
        if 'p' in res.keys():
            res.pop('p')
        for k,v in res.items():
            args += '&'+ k + '=' + v
    print(args)
    # {'types': 'all', 'keywords': '张三'}
    # &types=all&keywords=张三
    # # <li class="am-active"><a href="?p={{ i }}">{{ i }}</a></li>
    # # <li class="am-active"><a href="?p={{ i }}">{{ i }}</a></li>
    start = page-5
    end = page + 4
    if page < 7:
        start = 1
        end = 10
    if page > count-4:
        start = count-9
        end = count
    if count < 10:
        start = 1
        end = count
    listvar = []
    strvar = ''
    for i in range(start,end+1):
        listvar.append(i)
    if page > 1:
        strvar += '<li><a href="?p='+str(page-1)+'{}">上一页</a></li>'.format(args,)
    for i in listvar:
        if page == i:
        # class="am-active"
            strvar += '<li class="am-active"><a href="?p='+str(i)+args+'">'+str(i)+'</a></li>'
        else:
            strvar += '<li><a href="?p='+str(i)+args+'">'+str(i)+'</a></li>'
    if page <count: 
        strvar += '<li><a href="?p='+str(page+1)+'{}">下一页</a></li>'.format(args,)
    return format_html(strvar)
    # return strvar